package application

import (
	"egoal.com/DistributionGo/repository"
)

type UserAppService struct {
	UserRepository *repository.UserRepository `inject:""`
}

func (u *UserAppService) GetAllUsers() ([]*UserDTO, error) {
	users, err := u.UserRepository.GetAllUsers()
	if err != nil {
		return nil, err
	}

	var userDTOs = make([]*UserDTO, 0)
	for _, value := range users {
		var userDTO = UserDTO{}
		userDTO.FromUser(value)

		userDTOs = append(userDTOs, &userDTO)
	}

	return userDTOs, err
}

func (u *UserAppService) GetById(id string) (*UserDTO, error) {
	user, err := u.UserRepository.GetById(id)
	if err != nil {
		return nil, err
	}

	var userDTO = UserDTO{}
	userDTO.FromUser(user)

	return &userDTO, nil
}

func (u *UserAppService) GetByUserName(userName string) (*UserDTO, error) {
	user, err := u.UserRepository.GetByUserName(userName)
	if err != nil {
		return nil, err
	}

	var userDTO = UserDTO{}
	userDTO.FromUser(user)

	return &userDTO, nil
}
